# coding:utf-8

import unittest
from datetime import datetime
from django.test.client import Client
from vleApp.models import *
from django.contrib.auth.models import User
from auxiliaries import lstAverage, getComment

#----------Teacher Registration Module----------
class TeacherRegistrationTestCase(unittest.TestCase):
    '''tests for teacher registration'''

    def setUp(self):
        pass

    #def testTeacherName(self):
     #   self.teacher1=Teacher.objects.create(username="Maestro1", password="secreto1")
     #   teacherName=Teacher.objects.all()[0].username
     #   self.assertEquals("Maestro1",teacherName)

    #def testTeacherPassword(self):
     #   self.teacher2=Teacher.objects.create(username="Maestro2", password="secreto2")
     #   teacherPass=Teacher.objects.all()[1].password
     #   self.assertEquals("secreto2",teacherPass)


#----------Feedback Module----------

class RetroQuizzesTestCase(unittest.TestCase):
    '''tests for the feedback module'''

    def setUp(self):
        self.newQuiz1=Quiz.objects.create(quizId='1',date=datetime.now())
        self.newQuiz2=Quiz.objects.create(quizId='2',date=datetime.now())

        #Comments
        self.quizComment70=FeedbackComment.objects.create(comment="sacaste 70 o mas", category="Quiz", minGrade=70)
        self.quizComment90=FeedbackComment.objects.create(comment="sacaste 90 o mas", category="Quiz", minGrade=90)
        self.quizForum50=FeedbackComment.objects.create(comment="foro de 50 o mas", category="Forum", minGrade=50)
        self.quizForum80=FeedbackComment.objects.create(comment="foro de 80 o mas", category="Forum", minGrade=80)


    def testQuizNotAnswered(self):
        self.newUser = User.objects.create_user("Prueba","prueba@mail.com","pruebaPass")
        self.newStudent1=Student.objects.create(name="Prueba", user=self.newUser, sesionTime=1000)
        lstPromedio=[x for i in AnsweredQuiz.objects.filter(student=self.newStudent1)]
        promedio=lstAverage(lstPromedio)
        self.assertEquals(promedio,0)

    def testOneQuizAnswered(self):
        self.newUser2 = User.objects.create_user("Prueba2","prueba2@mail.com","prueba2Pass")
        self.newStudent2=Student.objects.create(name="Prueba2", user=self.newUser2, sesionTime=1000)
        self.newAnsQuiz1=AnsweredQuiz.objects.create(student=self.newStudent2, quiz=self.newQuiz1, grade=80)

        lstPromedio=[x.grade for x in AnsweredQuiz.objects.filter(student=self.newStudent2)]
        promedio=lstAverage(lstPromedio)
        self.assertEquals(promedio,80)

    def testManyQuizAnswered(self):
        self.newUser3 = User.objects.create_user("Prueba3","prueba3@mail.com", "prueba3Pass")
        self.newStudent3=Student.objects.create(name="Prueba3", user=self.newUser3, sesionTime=1000)

        self.newAnsQuiz1=AnsweredQuiz.objects.create(student=self.newStudent3, quiz=self.newQuiz1, grade=80)
        self.newAnsQuiz1=AnsweredQuiz.objects.create(student=self.newStudent3, quiz=self.newQuiz2, grade=60)

        lstPromedio=[x.grade for x in AnsweredQuiz.objects.filter(student=self.newStudent3)]
        promedio=lstAverage(lstPromedio)

        self.assertEquals(promedio,70)

    def testNoCommentAvailableQuiz(self):
        comment=getComment("Quiz", 10)
        self.assertEquals(comment,"")

    def testNoCommentAvailableForum(self):
        comment=getComment("Forum", 10)
        self.assertEquals(comment,"")

    def testCommentQuizEqualToGrade(self):
        comment=getComment("Quiz", 70)
        self.assertEquals(comment.comment,"sacaste 70 o mas")

    def testCommentQuizLessThanGrade(self):
        comment=getComment("Quiz", 80)
        self.assertEquals(comment.comment,"sacaste 70 o mas")

    def testCommentQuizMoreThanGrade(self):
        comment=getComment("Quiz", 100)
        self.assertEquals(comment.comment,"sacaste 90 o mas")

    def testCommentForumEqualToGrade(self):
        comment=getComment("Forum", 50)
        self.assertEquals(comment.comment,"foro de 50 o mas")

    def testCommentForumLessThanGrade(self):
        comment=getComment("Forum", 70)
        self.assertEquals(comment.comment,"foro de 50 o mas")

    def testCommentForumMoreThanGrade(self):
        comment=getComment("Forum", 80)
        self.assertEquals(comment.comment,"foro de 80 o mas")

#----------Files Module----------

class UploadFileTestCase(unittest.TestCase):
    '''tests for file model'''

    def setUp(self):
        self.newFile=File.objects.create(title="titulo", description="descripcion", filename="filename", date=datetime.now())

    def testFileName(self):
        file=File.objects.all()[0]
        self.assertEquals("titulo",file.title)

    def testFileDescription(self):
        file=File.objects.all()[0]
        self.assertEquals("descripcion",file.description)

# ---------URLS-----------

def testUrl(url,test):
     '''generic url test'''
     c=Client()
     response=c.get(url)
     test.assertEquals(200,response.status_code)

class PageTestCase(unittest.TestCase):
    '''Test for pages (urls)'''

    def testStartPage(self):
        testUrl("/start/",self)
        
    def testRetroQuizPage(self):
        testUrl("/retro/quiz/prueba/",self)

    def testRetroForumPage(self):
        testUrl("/retro/forum/prueba/",self)

    def testUploadFilePage(self):
        testUrl("/uploadFile/",self)

    def testRegistrarAlumnoPage(self):
        testUrl("/registrar_alumno/",self)

    def testLoginPage(self):
        testUrl("/login/",self)

    def testCheckSessionPage(self):
        testUrl("/home/checkSession/",self)

    def testLogoutPage(self):
        #testUrl("/home/logout/",self)
        pass

    def testHomePage(self):
        testUrl("/home/",self)

    def testViewVideoPage(self):
        ###video must be uploaded before
        testUrl("/viewVideo/videoMB.flv/",self)

    def testViewVideoMenu(self):
        testUrl("/viewVideoMenu/",self)

    def testFileMenu(self):
        testUrl("/fileMenu/",self)

    def testAdminPage(self):
        testUrl("/admin/",self)

    def testFeedbackPage(self):
        testUrl("/editar_retroalimentacion/",self)

    def testTeacherRegistrationPage(self):
        testUrl("/registrarMaestro/",self)

    def testPermissionDeniedPage(self):
        testUrl("/permissionDenied/",self)
        

